Skip to content

Conversation

mtrofin
Copy link
Member

@mtrofin mtrofin commented Sep 15, 2025

No description provided.

Copy link
Member Author

mtrofin commented Sep 15, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@mtrofin mtrofin marked this pull request as ready for review September 15, 2025 18:02
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2025

@llvm/pr-subscribers-llvm-adt

Author: Mircea Trofin (mtrofin)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/158702.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/STLExtras.h (+6)
  • (modified) llvm/unittests/ADT/STLExtrasTest.cpp (+11)
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 099bef288b953..155feb90bd94c 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -35,6 +35,7 @@
 #include <iterator>
 #include <limits>
 #include <memory>
+#include <numeric>
 #include <optional>
 #include <tuple>
 #include <type_traits>
@@ -1694,6 +1695,11 @@ template <typename R> constexpr size_t range_size(R &&Range) {
     return static_cast<size_t>(std::distance(adl_begin(Range), adl_end(Range)));
 }
 
+/// Wrapper for std::accumulate.
+template <typename R, typename E> E range_accumulate(R &&Range, E Init) {
+  std::accumulate(Range.begin(), Range.end(), Init);
+}
+
 /// Provide wrappers to std::for_each which take ranges instead of having to
 /// pass begin/end explicitly.
 template <typename R, typename UnaryFunction>
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 9fda0d912a2f5..ff1d413979bdc 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1602,6 +1602,17 @@ TEST(STLExtrasTest, Fill) {
   EXPECT_THAT(V2, ElementsAre(Val, Val, Val, Val));
 }
 
+TEST(STLExtrasTest, Accumulate) {
+  EXPECT_EQ(range_accumulate(std::vector<int>(), 0), 0);
+  EXPECT_EQ(range_accumulate(std::vector<int>(), 3), 3);
+  std::vector<int> V1 = {1, 2, 3, 4, 5};
+  EXPECT_EQ(range_accumulate(V1, 0), std::accumulate(V1.begin(), V1.end(), 0));
+  EXPECT_EQ(range_accumulate(V1, 10),
+            std::accumulate(V1.begin(), V1.end(), 10));
+  EXPECT_EQ(range_accumulate(drop_begin(V1), 7),
+            std::accumulate(V1.begin(), V1.end(), 7) - V1[0]);
+}
+
 struct Foo;
 struct Bar {};
 

@mtrofin mtrofin force-pushed the users/mtrofin/09-15-_adt_wrapper_for_std_accumulate_accepting_a_range_ branch from dc3518f to 454d85e Compare September 15, 2025 18:20
@mtrofin mtrofin force-pushed the users/mtrofin/09-15-_adt_wrapper_for_std_accumulate_accepting_a_range_ branch 2 times, most recently from 5713917 to 588913f Compare September 15, 2025 19:11
Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@mtrofin mtrofin force-pushed the users/mtrofin/09-15-_adt_wrapper_for_std_accumulate_accepting_a_range_ branch from 588913f to 277dcd5 Compare September 15, 2025 20:51
Copy link
Member Author

mtrofin commented Sep 16, 2025

Merge activity

  • Sep 16, 2:18 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Sep 16, 2:20 PM UTC: @mtrofin merged this pull request with Graphite.

@mtrofin mtrofin merged commit 7e71877 into main Sep 16, 2025
9 checks passed
@mtrofin mtrofin deleted the users/mtrofin/09-15-_adt_wrapper_for_std_accumulate_accepting_a_range_ branch September 16, 2025 14:20
felipepiovezan pushed a commit to felipepiovezan/llvm-project that referenced this pull request Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants